package com.chenxin.transaction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chenxin.transaction.dao.LisiAccountInfoDao;
import com.chenxin.transaction.entity.AccountChangeEvent;
import com.chenxin.transaction.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @title: ZhangsanTxmsgAccountServieImpl
 * @Author Star_Chen
 * @Date: 2022/5/31 11:37
 * @Version 1.0
 */
@Service("lisiTxmsgAccountServiceImpl")
@Slf4j
public class LisiTxmsgAccountServiceImpl implements AccountInfoService {

    @Autowired
    private LisiAccountInfoDao accountInfoDao;

    @Override
    public void updateAccountBalance(String accountNo, Double amount) {
        return;
    }

    @Override
    public void sendUpdateAccountBalance(AccountChangeEvent accountChangeEvent) {
        return;
    }

    @Override
    @Transactional
    public void doUpdateAccountBalance(AccountChangeEvent accountChangeEvent) {

        log.info("bank2更新本地账号，账号：{},金额：{}",accountChangeEvent.getAccountNo(),accountChangeEvent.getAmount());

        //幂等判断,如果存在此事务，就无需更新
        if (accountInfoDao.isExistTx(accountChangeEvent.getTxNo()) > 0) {
            return;
        }

        //扣减金额
        accountInfoDao.updateAccountBalance(accountChangeEvent.getAccountNo(), accountChangeEvent.getAmount());
        accountInfoDao.addTx(accountChangeEvent.getTxNo());
        if (accountChangeEvent.getAmount() == 3) {
            throw new RuntimeException("人为制造异常！");
        }
    }
}
